//
//  RootViewController.swift
//  SwiftYC
//
//  Created by tenltrs on 2018/9/6.
//  Copyright © 2018年 Tenltrs. All rights reserved.
//

import UIKit

class RootViewController: UITabBarController,UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        
        addItem(childVc: HomeViewController(), title: "首页", image: "ic_tabbar_home_normal", selectedImage: "ic_tabbar_home_pressed")
        addItem(childVc: FoundViewController(), title: "发现", image: "ic_tabbar_discover_normal", selectedImage: "ic_tabbar_discover_pressed")
        addItem(childVc: WorldViewController(), title: "世界", image: "ic_tabbar_media_normal", selectedImage: "ic_tabbar_media_pressed")
        addItem(childVc: MineViewController(), title: "我的", image: "ic_tabbar_me_normal", selectedImage: "ic_tabbar_me_pressed")
        self.delegate = self;
        
        
        self.tabBar.backgroundImage = UIImage(named: "Tabbar-Bg")?.resizableImage(withCapInsets: UIEdgeInsets(top: 10, left: 1, bottom: 10, right: 1))
        self.tabBar.shadowImage=UIImage()
        
//        let shadowView = UIView.init(frame: CGRect(x: 0, y: 0, width: ScreenWidth, height: 0.5))
//        shadowView.backgroundColor=UIColor(hexString: "#999999")
//        self.tabBar.addSubview(shadowView)
    }
    
    func addItem(childVc:UIViewController, title:String, image:String, selectedImage:String) {
        
        childVc.title=title
        childVc.tabBarItem.image=UIImage(named: image)?.withRenderingMode(.alwaysOriginal)
        childVc.tabBarItem.selectedImage=UIImage(named: selectedImage)?.withRenderingMode(.alwaysOriginal)
        
        let att : [NSAttributedString.Key : Any] = [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 12)]
        let attSelect : [NSAttributedString.Key : Any] = [NSAttributedString.Key.foregroundColor:UIColor.NavColor]
        
        childVc.tabBarItem.setTitleTextAttributes(att, for: .normal)
        childVc.tabBarItem.setTitleTextAttributes(attSelect, for: .selected)
        
        let Nav = RootNavigationController(rootViewController: childVc)
        self.addChild(Nav)
    }
    
    // MARK: -- 点击动画
    func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
        
        var btnArr = Array<UIControl>()
        
        for tabBarButton in self.tabBar.subviews {

            if tabBarButton.isKind(of: NSClassFromString("UITabBarButton")!){
                btnArr.append(tabBarButton as! UIControl)
            }
        }
        
        self.tabBarButtonClick(tabBarButton: btnArr[self.selectedIndex] )
    }
    
    func tabBarButtonClick(tabBarButton : UIControl) {
        
        let animation = CAKeyframeAnimation()
        animation.keyPath="transform.scale"
        animation.values=[1,1.1,0.9,1]
        animation.duration=0.3
        animation.calculationMode = .cubic
        tabBarButton.layer.add(animation, forKey: nil)
    }

    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
